package com.baiyun.lease.web.admin.service.impl;

import com.baiyun.lease.common.constant.RedisConstant;
import com.baiyun.lease.model.entity.*;
import com.baiyun.lease.model.enums.ItemType;
import com.baiyun.lease.model.enums.ReleaseStatus;
import com.baiyun.lease.web.admin.mapper.*;
import com.baiyun.lease.web.admin.service.*;
import com.baiyun.lease.web.admin.vo.attr.AttrValueVo;
import com.baiyun.lease.web.admin.vo.graph.GraphVo;
import com.baiyun.lease.web.admin.vo.room.RoomDetailVo;
import com.baiyun.lease.web.admin.vo.room.RoomItemVo;
import com.baiyun.lease.web.admin.vo.room.RoomQueryVo;
import com.baiyun.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        this.saveOrUpdate(roomSubmitVo);
        //如果id不为null，则为更新操作，所以需要将原有的数据进行删除操作再新的信息进行更新操作
        if (id!=null){
            //1.删除图片列表
            graphInfoService.lambdaUpdate()
                    .eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId,id)
                    .remove();
            //2.删除属性信息列表
            roomAttrValueService.lambdaUpdate()
                    .eq(RoomAttrValue::getRoomId,id)
                    .remove();
            //3.删除配套信息列表
            roomFacilityService.lambdaUpdate()
                    .eq(RoomFacility::getRoomId,id)
                    .remove();
            //4.删除标签信息列表
            roomLabelService.lambdaUpdate()
                    .eq(RoomLabel::getRoomId,id)
                    .remove();
            //5.删除支付列表
            roomPaymentTypeService.lambdaUpdate()
                    .eq(RoomPaymentType::getRoomId,id)
                    .remove();
            //6.删除可选租期列表
            roomLeaseTermService.lambdaUpdate()
                   .eq(RoomLeaseTerm::getRoomId,id)
                   .remove();
            //7.删除缓存,如果房间信息进行更新，需要将redis中的缓存删除
            redisTemplate.delete(RedisConstant.APP_LOGIN_PREFIX + roomSubmitVo.getId());
        }
        //1.添加图片列表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //2.添加属性信息列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            ArrayList<RoomAttrValue> arrayList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                arrayList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(arrayList);
        }
        //3.添加配套信息列表
        List<Long> facilityIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityIds)){
            ArrayList<RoomFacility> arrayList = new ArrayList<>();
            for (Long facilityId : facilityIds) {
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(facilityId);
                arrayList.add(roomFacility);
            }
            roomFacilityService.saveBatch(arrayList);
        }
        //4.添加标签信息列表
        List<Long> labelIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            ArrayList<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelId : labelIds) {
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setRoomId(roomSubmitVo.getId());
                roomLabel.setLabelId(labelId);
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        //5.添加支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = new RoomPaymentType();
                roomPaymentType.setRoomId(roomSubmitVo.getId());
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //6.添加可租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                roomLeaseTerm.setRoomId(roomSubmitVo.getId());
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }

    }

    @Override
    public IPage<RoomItemVo> pageItem(long current, long size, RoomQueryVo queryVo) {
        Page<RoomItemVo> page = new Page(current,size);
        IPage<RoomItemVo> pageApartmentItemVo = roomInfoMapper.pageItem(page,queryVo);
        return pageApartmentItemVo;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //1.查询房间信息
        RoomInfo roomInfo = this.getById(id);
        if (ObjectUtils.isEmpty(roomInfo)){
            return null;
        }
        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //3.查询房间图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
        //4.查询房间属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        //6.查询房间标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListBYRoomId(id);
        //7.查询房间支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        //8.查询房间可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeDetailById(Long id) {
        //1.删除房间基本信息
        this.removeById(id);
        //2.删除房间图片列表graphVoList
        graphInfoService.lambdaUpdate()
                .eq(GraphInfo::getItemType,ItemType.ROOM)
                .eq(GraphInfo::getItemId,id)
                .remove();
        //3.删除属性信息列表attrValueVoList
        roomAttrValueService.lambdaUpdate()
                .eq(RoomAttrValue::getRoomId,id)
                .remove();
        //4.删除房间配套信息列表facilityInfoList
        roomFacilityService.lambdaUpdate()
                .eq(RoomFacility::getRoomId,id)
                .remove();
        //5.删除房间标签信息列表labelInfoList
        roomLabelService.lambdaUpdate()
                .eq(RoomLabel::getRoomId,id)
                .remove();
        //6.删除房间支付方式列表paymentTypeList
        roomPaymentTypeService.lambdaUpdate()
                .eq(RoomPaymentType::getRoomId,id)
                .remove();
        //7.删除房间可选租期列表leaseTermList
        roomLeaseTermService.lambdaUpdate()
                .eq(RoomLeaseTerm::getRoomId,id)
                .remove();
        //8.删除缓存，房间删除，redis中的缓存也删除
        redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + id);
    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        this.lambdaUpdate()
                .eq(RoomInfo::getId,id)
                .set(RoomInfo::getIsRelease,status)
                .update();
    }
}




